package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.UserRepayService;
import com.woniu.p2p.entity.Loans;
import com.woniu.p2p.entity.User;
import com.woniu.p2p.listener.InRepayListener;
import com.woniu.p2p.vo.UserAndLoansVo;
import com.woniu.p2p.vo.UserRepayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户还贷月份管理
 */
@Controller
@RequestMapping("/myrepay")
public class UserRepayController {

    @Autowired
    UserRepayService userRepayService;

    @RequestMapping("/getUserRepay")
    public String getUserRepay(Map map, String repaystatus,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") Date predate,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") Date nextdate,
                               String repaykeyword,
                               Integer pagenum,
                               Integer pagesize){
        pagenum=pagenum==null?1:pagenum;
        Integer num=pagesize==null?3:pagesize;
        PageInfo<UserRepayVo> pageInfo = userRepayService.findAllUserRepayForPageInfo(pagenum, num, repaystatus, predate, nextdate, repaykeyword);
        map.put("userrepay",pageInfo);
        map.put("repaystatus",repaystatus);
        map.put("predate",predate);
        map.put("nextdate",nextdate);
        map.put("repaykeyword",repaykeyword);
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        return "content/repay";
    }


    @RequestMapping("/getdetail")
    @ResponseBody
    public UserAndLoansVo getUser(Map map, Integer id, Integer id2){
        User allUserById = userRepayService.findAllUserById(id);
        Loans allLoansById = userRepayService.findAllLoansById(id2);
        String userHead = allUserById.getUserHead();
        Long userAmount = allUserById.getUserAmount();
        String userRealname = allUserById.getUserRealname();
        Integer userCode = allUserById.getUserCode();
        Long userGetmoney = allUserById.getUserGetmoney();
        String loansName = allLoansById.getLoansName();
        Long loansMoney = allLoansById.getLoansMoney();
        Date loansTime = allLoansById.getLoansTime();
        String loansPhone = allLoansById.getLoansPhone();
        String loansState = allLoansById.getLoansState();
        String loansMonth = allLoansById.getLoansMonth();
        Long loansTotalvalue = allLoansById.getLoansTotalvalue();

        UserAndLoansVo userAndLoansVo = new UserAndLoansVo( userHead,  userAmount,  userRealname,  userCode,  userGetmoney,  loansName,
                loansMoney,  loansTime,  loansPhone,  loansState,  loansMonth,  loansTotalvalue);
        return userAndLoansVo;
    }


    @RequestMapping("/delonerepay")
    public int delonerepay(int id){
        int i = userRepayService.deleteUserRepay(id);
        return i;
    }


    @RequestMapping("/delallrepay")
    public int deloneafterloans(int[] id){
        int m=1;
        for (int i = 0; i <id.length ; i++) {
            int i1 = userRepayService.deleteUserRepay(id[i]);
            if(i1==0){
                m=0;
                break;
            }else if(i1==0){
                m=1;
            }
        }
        return m;
    }

    /**
     * 导出
     */
    @RequestMapping("/outputrepay")
    public void outputrepay(int[] ids,
                                 HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");


        List<UserRepayVo> list = userRepayService.findAllMonloans(ids);
        System.out.println(list);
        ExcelWriterBuilder write = EasyExcel.write(outputStream, UserRepayVo.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(list);
        outputStream.close();
    }


    @Autowired
    InRepayListener inRepayListener;

    /**
     * 导入
     */
    @RequestMapping("/inrepay")
    @ResponseBody
    public Integer inrepay(@RequestParam("file") MultipartFile file){
        try {
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), UserRepayVo.class,inRepayListener);
            ExcelReaderSheetBuilder sheet = read.sheet();
            sheet.doRead();

        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
